package com.mohunhun.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 通用响应结果封装类
 *
 * @param <T> 数据类型
 */
public class AppResult<T> {

    // 状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private long code;


    // 错误描述
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;

    // 结果数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    // 私有构造
    private AppResult(long code, String message) {
        this(code, message, null);
    }

    private AppResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 成功 - 无数据
    public static <T> AppResult<T> success() {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    // 成功 - 自定义消息
    public static <T> AppResult<T> success(String message) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message);
    }

    // 成功 - 包含数据
    public static <T> AppResult<T> success(T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    // 成功 - 自定义消息 + 数据
    public static <T> AppResult<T> success(String message, T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    // 失败 - 无数据
    public static <T> AppResult<T> failed() {
        return new AppResult<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    // 失败 - 自定义消息
    public static <T> AppResult<T> failed(String message) {
        return new AppResult<>(ResultCode.FAILED.getCode(), message);
    }

    // 失败 - 包含数据（如错误详情）
    public static <T> AppResult<T> failed(T data) {
        return new AppResult<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), data);
    }

    public static <T> AppResult<T> failed(ResultCode code) {
        return new AppResult<>(code.getCode(), code.getMessage(), null);
    }

    // 失败 - 自定义消息 + 数据
    public static <T> AppResult<T> failed(String message, T data) {
        return new AppResult<>(ResultCode.FAILED.getCode(), message, data);
    }

    // getter / setter
    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // toString 方法（便于调试）
    @Override
    public String toString() {
        return "AppResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}